package com.group4.admin.beans;

import com.group4.controller.ParentFacade;
import com.group4.controller.StudentFacade;
import com.group4.entity.Parent;
import com.group4.entity.Student;
import com.group4.entity.User;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author andake, anjonas
 */
@ManagedBean
@RequestScoped
public class AdminParentsBackingBean {

    @EJB
    private ParentFacade parentDb;
    @EJB
    private StudentFacade studentDb;
    private User user;
    private List<String> childrenId;

    /** Creates a new instance of AdminParentsBackingBean */
    public AdminParentsBackingBean() {
        user = new User();
    }
    
    // adds a parent
    public void addParent() {
        Parent p = new Parent();
        p.setUser(user);

        List<Student> children = new ArrayList<Student>();
        for (String id : childrenId) {
            children.add(studentDb.find(Long.valueOf(id)));
        }
        
        p.setChildren(children);
        parentDb.create(p);
        user = new User();
    }
    
    // deletes a parent
    public void deleteParent(Parent p) {
        parentDb.remove(p);
    }

    public List<Parent> getParents() {
        return parentDb.findAll();
    }

    public List<Student> getStudents() {
        return studentDb.findAll();
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<String> getChildrenId() {
        return childrenId;
    }

    public void setChildrenId(List<String> childrenId) {
        this.childrenId = childrenId;
    }
}
